<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Institucion extends MN_Controller {

    function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->helper('url');
        $this->load->library('ion_auth');
        $this->load->library('session');
        $this->load->model('Institucion_Model');
    }

    public function index() {

      if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin()   ) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();
        //redirect('auth', 'refresh');
        //$this->_render('home/inicio');
    }

    public function inicio() {
      if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin()   ) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();
        
        $this->data['institucion'] = $this->Institucion_Model->getListado();
        $this->_render('institucion/start');
        //redirect('auth', 'refresh');
    }

    public function crear() {
      if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin()   ) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();
        
        $this->load->helper(array('url', 'form'));
        $this->load->library('form_validation');
        $this->_render('institucion/new');

        //redirect('auth', 'refresh');
    }

    public function actualizar() {
      if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin()   ) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();
        
        $this->load->helper(array('url', 'form'));
        $this->load->library('form_validation');

        //$this->data['institucion']=$this->Institucion_Model->getDatosInstitucion($ide);
        $opc = $this->uri->segment(3);
        $ide = $this->uri->segment(4);
        if (!isset($_POST['txtrep'])) {
            $this->data['institucion'] = $this->Institucion_Model->getDatosInstitucion($ide);
            $this->data['opc'] = $opc;
            $this->_render('institucion/update');
        } else {
            $ide = $this->input->post('txtide');
            $opc = $this->input->post('txtopc');
            $this->data['institucion'] = $this->Institucion_Model->getDatosInstitucion($ide);
            $this->data['opc'] = $opc;

            $this->form_validation->set_rules('txtrep', 'Representante', 'required|trim');
            $this->form_validation->set_rules('txtnom', 'Nombre', 'required|trim');
            $this->form_validation->set_rules('txtciu', 'Ciudad', 'required|trim');
            $this->form_validation->set_rules('txtdir', 'Direccion', 'required|trim');
            $this->form_validation->set_rules('txtema', 'Email', 'trim|required|valid_email');
            $this->form_validation->set_rules('cuenta1_inst', 'Cuenta1', 'trim|required');
            $this->form_validation->set_rules('banco1_inst', 'Banco1', 'trim|required');
            $this->form_validation->set_rules('cuenta2_inst', 'Cuenta2', 'trim|required');
            $this->form_validation->set_rules('banco2_inst', 'Banco2', 'trim|required');

            if (($this->form_validation->run() == FALSE)) {
                $this->_render('institucion/update');
                //redirect("institucion/actualizar/".$opc."/".$ide);
            } else {
                $institucion = array(
                    "repr_legal_int" => $this->input->post('txtrep'),
                    "nombre_inst" => $this->input->post('txtnom'),
                    "direccion_inst" => $this->input->post('txtdir'),
                    "ruc_inst" => $this->input->post('txtruc'),
                    "fono_inst" => $this->input->post('txtfon'),
                    "logo_inst" => $this->input->post('txtlog'),
                    "url_inst" => $this->input->post('txturl'),
                    "email_inst" => $this->input->post('txtema'),
                    "ciudad_inst" => $this->input->post('txtciu'),
                    "cuenta1_inst" => $this->input->post('cuenta1_inst'),
                    "banco1_inst" => $this->input->post('banco1_inst'),
                    "cuenta2_inst" => $this->input->post('cuenta2_inst'),
                    "banco2_inst" => $this->input->post('banco2_inst'),
                    "cuenta3_inst" => $this->input->post('cuenta3_inst'),
                    "banco3_inst" => $this->input->post('banco3_inst'));
                //print_r($institucion);
                if ($opc == 'upd')
                    $this->Institucion_Model->actualizarInstitucion($institucion, $ide);
                else
                    $this->Institucion_Model->eliminarInstitucion($ide);
                redirect('institucion/inicio');
  
            }
        }
    }

    public function ingresodatos() {
      if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin()   ) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();

        $this->load->helper(array('url', 'form'));
        $this->load->library('form_validation');

        $this->form_validation->set_rules('txtrep', 'Representante', 'required|trim');
        $this->form_validation->set_rules('txtnom', 'Nombre', 'required|trim');
        $this->form_validation->set_rules('txtciu', 'Ciudad', 'required|trim');
        $this->form_validation->set_rules('txtdir', 'Direccion', 'required|trim');
        $this->form_validation->set_rules('txtema', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('cuenta1_inst', 'Cuenta1', 'trim|required');
        $this->form_validation->set_rules('banco1_inst', 'Banco1', 'trim|required');
        $this->form_validation->set_rules('cuenta2_inst', 'Cuenta2', 'trim|required');
        $this->form_validation->set_rules('banco2_inst', 'Banco2', 'trim|required');


        if (($this->form_validation->run() == FALSE)) {
            $this->_render('institucion/new');
        } else {
            $institucion = array(
                "repr_legal_int" => $this->input->post('txtrep'),
                "nombre_inst" => $this->input->post('txtnom'),
                "direccion_inst" => $this->input->post('txtdir'),
                "ruc_inst" => $this->input->post('txtruc'),
                "fono_inst" => $this->input->post('txtfon'),
                "logo_inst" => $this->input->post('txtlog'),
                "url_inst" => $this->input->post('txturl'),
                "email_inst" => $this->input->post('txtema'),
                "ciudad_inst" => $this->input->post('txtciu'),
                "cuenta1_inst" => $this->input->post('cuenta1_inst'),
                "banco1_inst" => $this->input->post('banco1_inst'),
                "cuenta2_inst" => $this->input->post('cuenta2_inst'),
                "banco2_inst" => $this->input->post('banco2_inst'),
                "cuenta3_inst" => $this->input->post('cuenta3_inst'),
                "banco3_inst" => $this->input->post('banco3_inst'));
            $this->Institucion_Model->grabarInstitucion($institucion);
            redirect('institucion/inicio', 'refresh');
        }
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
